<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在站点" prop="siteId">
        <el-select @change="handleQuery" v-model="queryParams.siteId" placeholder="请选择所在站点">
          <el-option v-for="(dict,index) in siteList" :key="index" :label="dict.name" :value="dict.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="题目" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入题目" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="题目类型" prop="topicType">
        <el-select v-model="queryParams.topicType" placeholder="请选择题目类型" clearable>
          <el-option v-for="dict in dict.type.app_topic_type" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['applet:siteTopic:add']">新增
        </el-button>
      </el-form-item>
    </el-form>


    <el-table border stripe v-loading="loading" :data="siteTopicList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <!--      <el-table-column label="主键" align="center" prop="id"/>-->

      <el-table-column show-overflow-tooltip label="题目" width="220" align="center" prop="title"/>
      <el-table-column show-overflow-tooltip label="类型" width="100" align="center" prop="topicType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_topic_type" :value="scope.row.topicType"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="顺序" width="80" align="center" prop="topicOrder"/>
      <el-table-column show-overflow-tooltip label="答案" width="80" align="center" prop="tanser"/>
      <el-table-column show-overflow-tooltip label="选项A" width="150" align="center" prop="choose1"/>
      <el-table-column show-overflow-tooltip label="选项B" width="150" align="center" prop="choose2"/>
      <el-table-column show-overflow-tooltip label="选项C" width="150" align="center" prop="choose3"/>
      <el-table-column show-overflow-tooltip label="选项D" width="150" align="center" prop="choose4"/>
      <el-table-column show-overflow-tooltip label="解析" align="center" prop="parse"/>
      <el-table-column show-overflow-tooltip label="所在站点" width="130" align="center" prop="siteName"/>
      <el-table-column show-overflow-tooltip label="备注说明" align="center" prop="remark"/>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:siteTopic:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:siteTopic:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改站点题目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="题目类型" prop="topicType">
              <el-select v-model="form.topicType" placeholder="请选择题目类型">
                <el-option
                  v-for="dict in dict.type.app_topic_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="题目顺序" prop="topicOrder">
              <el-input-number :min="0" v-model="form.topicOrder" placeholder="请输入题目顺序"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="正确答案" prop="tanser">
              <el-input v-model="form.tanser" placeholder="请输入正确答案"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属站点" prop="siteId">
              <el-select v-model="form.siteId" placeholder="请选择所属站点">
                <el-option v-for="(dict,index) in siteList" :key="index" :label="dict.name" :value="dict.id"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="题目" prop="title">
          <el-input v-model="form.title" placeholder="请输入题目"/>
        </el-form-item>

        <el-form-item label="选项1" prop="choose1">
          <el-input v-model="form.choose1" placeholder="请输入选项1"/>
        </el-form-item>
        <el-form-item label="选项2" prop="choose2">
          <el-input v-model="form.choose2" placeholder="请输入选项2"/>
        </el-form-item>
        <el-form-item label="选项3" prop="choose3">
          <el-input v-model="form.choose3" placeholder="请输入选项3"/>
        </el-form-item>
        <el-form-item label="选项4" prop="choose4">
          <el-input v-model="form.choose4" placeholder="请输入选项4"/>
        </el-form-item>


        <el-form-item label="解析" prop="parse">
          <el-input v-model="form.parse" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addSiteTopic, delSiteTopic, getSiteTopic, listSiteTopic, updateSiteTopic} from "@/api/background/siteTopic";
import {selectAppSite} from "@/api/background/appSite";

export default {
  name: "SiteTopic",
  dicts: ['app_topic_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点题目表格数据
      siteTopicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "题目不能为空", trigger: "blur"}
        ], tanser: [
          {required: true, message: "正确答案不能为空", trigger: "blur"}
        ], choose1: [
          {required: true, message: "选项1不能为空", trigger: "blur"}
        ], choose2: [
          {required: true, message: "选项2不能为空", trigger: "blur"}
        ], topicType: [
          {required: true, message: "请选择题目类型", trigger: "blur"}
        ], topicOrder: [
          {required: true, message: "题目顺序不能为空", trigger: "blur"}
        ]
      },
      siteList: [],
    };
  },
  created() {
    this.getSiteSelect()
  },
  methods: {
    getSiteSelect() {
      selectAppSite().then(res => {
        this.siteList = res.data
        if (this.siteList && this.siteList.length > 0) {
          this.queryParams.siteId = this.siteList[0].id
          this.getList();
        } else {
          this.loading = false
        }
      })
    },

    /** 查询站点题目列表 */
    getList() {
      this.loading = true;
      listSiteTopic(this.queryParams).then(response => {
        this.siteTopicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        title: null,
        tanser: null,
        choose1: null,
        choose2: null,
        choose3: null,
        choose4: null,
        topicType: null,
        topicOrder: null,
        parse: null,
        updateTime: null,
        updateBy: null,
        createTime: null,
        createBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSiteTopic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改站点题目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSiteTopic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSiteTopic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除站点题目编号为"' + ids + '"的数据项？').then(function () {
        return delSiteTopic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/siteTopic/export', {
        ...this.queryParams
      }, `siteTopic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
